var publicApi = require('../../script/public.js');
var orderApi = require('../../script/order.js');
import tool from '../../utils/common.js';
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    UserList: [],
    SearchParams: {
      Area_Code_Dept: '',
      DEPT_NAME: '',
      Area_Code_Building: '',
      LOU_NAME: '',
      Area_Code_Unit: '',
      CONS_NO: '',
      CONS_NAME: '',
      PhoneNumber: '',
      Room: '',
    },
    pagination: {
      page: 1,
      rows: 30,
      sidx: '',
      sord: ''
    },
    HasNextPage: true,
    noData: false,
    isReachBottom: false,
    // 弹窗
    ShowPop: false,
    DeptShow: false,
    DeptList: [],
    DeptListShow: [],
    BuildingShow: false,
    BuildingList: [],
    BuildingListShow: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this._refreshData();
    this.getDepts();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      isReachBottom: true
    });
    if (this.data.HasNextPages) {
      this.setData({
        'pagination.page': this.data.pagination.page + 1
      })
      this.searchUser();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  _refreshData() {
    this.setData({
      pagination: {
        page: 1,
        rows: 30,
        sidx: '',
        sord: ''
      },
      SearchParams: {
        Area_Code_Dept: '',
        DEPT_NAME: '',
        Area_Code_Building: '',
        LOU_NAME: '',
        Area_Code_Unit: '',
        CONS_NO: '',
        CONS_NAME: '',
        PhoneNumber: '',
        Room: ''
      },
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
      BuildingList: [],
    });
    this.searchUser(true);
  },
  searchChange: tool.debounce(function (e) {
    const ev = e[0];
    this.setData({
      'SearchParams.CONS_NO': ev.detail.value,
    });
    this.doSearch();
  }, 600),
  openFilter() {
    this.setData({
      ShowPop: true
    });
  },
  getDepts() {
    const params = {
      queryParam: {
        DropType: 1,
        Param_0: ''
      },
      pagination: {
        rows: 10000,
        page: 1,
        sidx: "",
        sord: ""
      }
    }
    publicApi.GetDropDownList_Page(params).then(res => {
      if (res.code === 200) {
        this.setData({
          DeptList: res.data.Items,
          DeptListShow: res.data.Items,
        });
      }
    })
  },
  getBuilding() {
    const params = {
      queryParam: {
        DropType: 3,
        Param_0: '',
        Param_1: this.data.SearchParams.Area_Code_Dept,
        Param_2: ""
      },
      pagination: {
        rows: 10000,
        page: 1,
        sidx: "",
        sord: ""
      }
    }
    publicApi.GetDropDownList_Page(params).then(res => {
      if (res.code === 200) {
        this.setData({
          BuildingList: res.data.Items,
          BuildingListShow: res.data.Items,
        });
      }
    })
  },
  openSearch() {
    this.setData({
      ShowPop: true
    });
  },
  leave() {},
  searchUser(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const params = {
      queryParam: {
        Area_Code_Dept: this.data.SearchParams.Area_Code_Dept,
        Area_Code_Building: this.data.SearchParams.Area_Code_Building,
        Area_Code_Unit: this.data.SearchParams.Area_Code_Unit,
        CONS_NO: this.data.SearchParams.CONS_NO,
        CONS_NAME: this.data.SearchParams.CONS_NAME,
        PhoneNumber: this.data.SearchParams.PhoneNumber,
        Room: this.data.SearchParams.Room,
      },
      pagination: {
        ...this.data.pagination
      }
    }
    orderApi.GetList_CC(params).then(res => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      wx.hideLoading();
      if (res.code === 200) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.data.Items]
        } else {
          dataList = [...this.data.UserList, ...res.data.Items]
        }
        const noData = dataList.length === 0
        this.setData({
          UserList: dataList,
          HasNextPages: res.data.HasNextPages,
          noData: noData
        })
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  showPopup(e) {
    const popupType = e.currentTarget.dataset.pop;
    if (popupType === 'Building' && this.data.SearchParams.Area_Code_Dept === '') {
      wx.showToast({
        title: '请先选择小区',
        icon: 'none'
      });
      return;
    }
    this.setData({
      [popupType + 'Show']: true
    });
  },
  closePop(e) {
    const popupType = e.currentTarget.dataset.pop;
    this.setData({
      [popupType + 'Show']: false,
      searchText: '',
      DeptListShow: [...this.data.DeptList],
      BuildingListShow: []
    });
  },
  searchDeptChange(e) {
    let val = e.detail.value;
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      const list = [...this.data.DeptList];
      const newList = list.filter(item => {
        return item.Label.indexOf(val) > -1
      })
      this.setData({
        searchText: val,
        DeptListShow: newList
      });
    })
  },
  selectDept(e) {
    const dept = e.currentTarget.dataset.info;
    this.setData({
      'SearchParams.Area_Code_Dept': dept.Value,
      'SearchParams.DEPT_NAME': dept.Label,
      DeptShow: false
    });
    this.getBuilding();
  },
  searchBuildingChange(e) {
    let val = e.detail.value;
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      const list = [...this.data.BuildingList];
      const newList = list.filter(item => {
        return item.Label.indexOf(val) > -1
      })
      this.setData({
        searchText: val,
        BuildingListShow: newList
      });
    })
  },
  selectBuilding(e) {
    const dept = e.currentTarget.dataset.info;
    this.setData({
      'SearchParams.Area_Code_Building': dept.Value,
      'SearchParams.LOU_NAME': dept.Label,
      BuildingShow: false
    });
  },
  inputChange(e) {
    const type = e.currentTarget.dataset.type;
    let val = e.detail.value;
    this.setData({
      ['SearchParams.' + type]: val
    });
  },
  reset() {
    this.setData({
      SearchParams: {
        Area_Code_Dept: '',
        DEPT_NAME: '',
        Area_Code_Building: '',
        LOU_NAME: '',
        Area_Code_Unit: '',
        CONS_NO: '',
        CONS_NAME: '',
        PhoneNumber: '',
        Room: ''
      },
    })
  },
  doSearch() {
    this.setData({
      pagination: {
        page: 1,
        rows: 30,
        sidx: '',
        sord: ''
      },
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
      ShowPop: false
    });
    this.searchUser(true);
  },
  toPage: function (e) {
    const route = e.currentTarget.dataset.route;
    const user = e.currentTarget.dataset.user;
    wx.navigateTo({
      url: '../' + route + '/' + route + '?CONS_NO=' + user.CONS_NO + '&CONS_NAME=' + user.CONS_NAME + '&addr='  + user.WATER_ADDR
    })
  }
})